package m.jclecai.com.ui;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.activity.LotteryJCLQActivity;
import m.jclecai.com.activity.LotteryJCZQActivity;
import m.jclecai.com.adapter.ZQ_LQ_LotteryDetailsaAdapter;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.MatchResultBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.DateUtil;
import m.jclecai.com.utils.ToastUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.view.CustomExpandListview;

/**
 * Created by hp on 2017/3/16.
 * 竞彩足球or竞彩篮球 开奖详情
 */

public class ZQ_LQ_LotteryDetailsActivity extends BaseActivity {

    private ZQ_LQ_LotteryDetailsaAdapter adapter;
    //处理后的日期数据
    private List<String> dateList;
    //处理后的比赛数据
    private List<List<MatchResultBean.Records>> recordsList;
    private List<MatchResultBean.Records> arr;

    @BindView(R.id.expand_list)
    CustomExpandListview expand_list;
    @BindView(R.id.bt_bottom_right)
    Button bt_bottom_right;
    @BindView(R.id.bt_bottom_left)
    Button bt_bottom_left;
    @BindView(R.id.tv_jjcProjectTwo)
    TextView tv_jjcProjectTwo;
    @BindView(R.id.tv_jjcProjectThree)
    TextView tv_jjcProjectThree;
    @BindView(R.id.tv_jjcProjectFour)
    TextView tv_jjcProjectFour;
    @BindView(R.id.tv_jjcProjectOne)
    TextView tv_jjcProjectOne;
    private String type;
    String sTime, eTime;

    @Override
    protected int getContentView() {
        return R.layout.zq_lq_lottery_details_activity;
    }

    @Override
    protected void initView() {
        setTitle("开奖详情");
        rightOptionText("日期");
        leftOptionText("");
        bt_bottom_right.setVisibility(View.GONE);
    }

    @Override
    protected void resetWindow() {
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        setTitle(intent.getStringExtra("typeName"));

        dateList = new ArrayList<>();
        recordsList = new ArrayList<>();
        adapter = new ZQ_LQ_LotteryDetailsaAdapter(this, dateList, recordsList, type, expand_list);
        //悬浮的标题
        expand_list.setHeaderView(getLayoutInflater().inflate(
                R.layout.zq_lq_lottery_details_group, expand_list, false));
        expand_list.setAdapter(adapter);
        if (type.equals("jczq")) {
            bt_bottom_left.setText("竞彩足球投注");
            tv_jjcProjectOne.setText("胜平负");
            tv_jjcProjectTwo.setText("让球胜平负");
            tv_jjcProjectThree.setText("总进球");
            tv_jjcProjectFour.setText("半全场");
        } else if (type.equals("jclq")) {
            bt_bottom_left.setText("竞彩篮球投注");
            tv_jjcProjectOne.setText("胜负");
            tv_jjcProjectTwo.setText("让分胜负");
            tv_jjcProjectThree.setText("大小分");
            tv_jjcProjectFour.setText("胜分差");
        } else if (type.equals("bjdc")) {
            bt_bottom_left.setText("北京单场投注");
            tv_jjcProjectOne.setText("胜平负");
            tv_jjcProjectTwo.setText("总进球");
            tv_jjcProjectThree.setText("半全场");
            tv_jjcProjectFour.setText("上下单双");
        }
        getMatchhResultData(type);
    }

    private void getMatchhResultData(String type) {
        showDialog();
        if ("bjdc".equals(type))
            type = "bjdc";
        Map<String, String> map = new HashMap<>();
        map.put("type", type);
        if (!TextUtils.isEmpty(sTime))
            map.put("stime", sTime);
        if (!TextUtils.isEmpty(eTime))
            map.put("etime", eTime);
        HttpManager.postAsyn(Constant_Url.MATCH_RESULT, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                ToastUtil.showToast(ZQ_LQ_LotteryDetailsActivity.this, R.string.connect_failure);
            }

            @Override
            public void onResponse(String response) {
                MatchResultBean bean = FastOk.get(response, MatchResultBean.class);
                setViewInfos(bean);
                dissDialog();
            }
        }, map);
    }

    private void setViewInfos(MatchResultBean bean) {
        try {
            dateList.clear();
            recordsList.clear();
            adapter.notifyDataSetChanged();
            if (bean == null) return;
            ArrayList<MatchResultBean.Records> records = bean.getRecords();
            dateList.add(records.get(0).getDate() + records.get(0).getWeek());
            //添加日期数据
            for (int i = 0; i < records.size(); i++) {
                String str = records.get(i).getDate() + records.get(i).getWeek();
                //循环添加JSON日期信息，判断日期不相同则添加
                for (int b = 0; b < dateList.size(); b++) {
                    if (!(dateList.get(dateList.size() - 1).equals(str))) {
                        dateList.add(str);
                    }
                }
            }

            //循环日期数据集合
            for (int i = 0; i < dateList.size(); i++) {
                arr = new ArrayList<>();
                //循环总数据集合
                for (int b = 0; b < records.size(); b++) {
                    //取出的日期数据与每条数据比较，相同日期的数据添加至同一集合
                    String str = records.get(b).getDate() + records.get(b).getWeek();
                    if (dateList.get(i).equals(str)) {
                        arr.add(records.get(b));
                    }
                }
                String title = dateList.get(i) + " " + arr.size() + "场可投";
                dateList.set(i, title);
                recordsList.add(arr);
            }
            adapter.notifyDataSetChanged();
            //默认展开所有item
            for (int i = 0; i < adapter.getGroupCount(); i++) {
                expand_list.expandGroup(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick(R.id.bt_bottom_left)
    void toLottery() {
        switch (type) {
            case "jczq":
                startActivity(new Intent(this, LotteryJCZQActivity.class));
                break;
            case "jclq":
                startActivity(new Intent(this, LotteryJCLQActivity.class));
                break;

        }
    }

    @OnClick(R.id.tv_right_option)
    void rightOption() {
        TimePickerView.Builder build = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                sTime = DateUtil.formatDate(date, "yyyy-MM-dd");
                eTime = sTime;
                getMatchhResultData(type);
            }
        });
        build.setType(TimePickerView.Type.YEAR_MONTH_DAY);
        TimePickerView pickerView = new TimePickerView(build);
        pickerView.show();
    }
}
